﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTOService;
using BUSService;
using COMExcel = Microsoft.Office.Interop.Excel;

namespace QuanLyBangDia
{
    public partial class frmBangDiaThueQuaHan : Form
    {
        //Thien code
        MyService myservice = new MyService();
        public frmBangDiaThueQuaHan()
        {
            InitializeComponent();
        }

        private void btnThongKe_Click(object sender, EventArgs e)
        {
            try
            {
                BangDia_Muon[] arr = myservice.ThongKeBangDiaQuanHan();
                dgvKQThongKe.Rows.Clear();
                for (int i = 0; i < arr.Length; i++)
                {
                    BangDia_Muon temp = arr[i];
                    BangDia bd = myservice.GetBangDia(temp.MaBD);
                    KhachHang kh = myservice.GetKhachHang(temp.MaKH);
                    Object[] o = { i + 1, temp.MaBD, bd.TenBD, kh.HoTen, temp.NgayMuon };
                    dgvKQThongKe.Rows.Add(o);
                }
            }
            catch (Exception ex)
            { MessageBox.Show(ex.Message); }
        }

        private void frmBangDiaThueQuaHan_Load(object sender, EventArgs e)
        {
            string h = DateTime.Now.ToLongTimeString();
            string n = DateTime.Now.ToLongDateString();
            string s = n + " " + h;
            lbNgayHienTai.Text = s.ToString();
        }

        private void btnIn_Click(object sender, EventArgs e)
        {
            // Khởi động chtr Excell
            COMExcel.Application file = new COMExcel.Application();
            System.Globalization.CultureInfo oldCI = System.Threading.Thread.CurrentThread.CurrentCulture;
            System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
            file.Visible = false;
            // Thêm file temp xls
            COMExcel.Workbook exBook = file.Workbooks.Add(COMExcel.XlWBATemplate.xlWBATWorksheet);

            // Lấy sheet 1.
            //COMExcel.Font = "Times New Roman";
            COMExcel.Worksheet sheet1 = (COMExcel.Worksheet)exBook.Worksheets[1];
            //sheet1.Activate();
            //sheet1.Select();
            sheet1.Select(0);
            sheet1.Name = "Thống Kê Băng Đĩa Thuê Quá Hạn";

            COMExcel.Range k = (COMExcel.Range)sheet1.Cells;
            k.Font.Name = "Times New Roman";
            COMExcel.Range cell = (COMExcel.Range)sheet1.Cells[2, 1];
            COMExcel.Range cell1 = (COMExcel.Range)sheet1.Cells[2, 4];
            cell.Value2 = "Thống Kê Băng Đĩa Thuê Quá Hạn";
            cell.Font.Bold = true;
            cell.Font.Size = 16;
            cell.Font.Color = Color.Red;
            cell.Font.Color = Color.Red;
            cell.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
            cell.VerticalAlignment = Microsoft.Office.Interop.Excel.XlVAlign.xlVAlignCenter;
            file.get_Range(cell, cell1).Merge(Type.Missing);
            string[] tieuDE = { "", "STT", "Mã Băng Đĩa", "Tên Băng Đĩa", "Họ Tên Khách Hàng", "Ngày   Thuê     " };
            for (int i = 1; i <= 5; i++)
            {
                COMExcel.Range cell2 = (COMExcel.Range)sheet1.Cells[5, i];
                cell2.Value2 = tieuDE[i];
                cell2.Font.Bold = true;
                cell2.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
                cell2.VerticalAlignment = Microsoft.Office.Interop.Excel.XlVAlign.xlVAlignCenter;
                cell2.Columns.AutoFit();
                cell2.Borders.Color = Color.Black;
            }
            for (int j = 0; j < dgvKQThongKe.Rows.Count; j++)
            {
                for (int i = 0; i < 5; i++)
                {
                    COMExcel.Range cell2 = (COMExcel.Range)sheet1.Cells[6 + j, 1 + i];
                    cell2.Value2 = dgvKQThongKe.Rows[j].Cells[i].Value;
                    cell2.Borders.Color = Color.Black;
                }
            }
            System.Threading.Thread.CurrentThread.CurrentCulture = oldCI;
            file.Visible = true;
        }

    }
}
